package com.nhc.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.location.GpsSatellite;
import android.util.AttributeSet;
import android.view.View;

import com.astrob.activitys.GPSProvider;
import com.astrob.util.SysUtils;
import com.astrob.util.Utils;

import java.util.Iterator;

public class GPS2StarView extends View {
    protected static final int UPGPSDATEMSG = 0x101;
    private Bitmap mbmpEarth = null;
    private Bitmap mbmpStar = null;

    private int mnStarRadius;
    private Paint mpaint;
    private Iterable<GpsSatellite> mgpsAllStatus = null;

    private int raduis;

    // 指北方向
    private float mfDegree;

    // 屏大小
    private int mnHegiht = 800;
    private int mnWidth = 480;

    // / 罗盘相对屏幕左边绘制的偏移量
    private int offset = 0;

    public GPS2StarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GPS2StarView(Context context) {
        super(context);
        init();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
                            int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        mnHegiht = (bottom - top);
        mnWidth = (right - left);
        updateOffset();
        raduis = Math.min(mnWidth, mnHegiht) / 2 - Utils.dip2px(getContext(), 3);
    }

    public void init() {
        this.mfDegree = 0;
        mnStarRadius = Utils.dip2px(getContext(), 13);
        if (SysUtils.getInstance().is240x320) {
            mnStarRadius = 8;
        }
        preparePaint();
    }

    protected void updateOffset() {
    }

    /**
     * 绘制精度圈的Paint
     */
    private Paint mMarkerPaint;
    private Paint mMarkerPaint2;
    private Paint mMarkerPaint3;
    private Paint mMarkerPaint4;

//    private Paint northPaint;
    private int northHeight;

//    private Paint textPaint;

    /**
     * 准备paint和车标图片
     */
    private void preparePaint() {

        // 大圆画笔
        mMarkerPaint = new Paint();
        mMarkerPaint.setAntiAlias(true);
        mMarkerPaint.setARGB(0xFF, 0xF7, 0xF9, 0xF9);
        mMarkerPaint.setStyle(Paint.Style.FILL);

        // 最外层画笔
        mMarkerPaint2 = new Paint();
        mMarkerPaint2.setAntiAlias(true);
        mMarkerPaint2.setARGB(0xFF, 0x72, 0xB2, 0xFA);
        mMarkerPaint2.setStyle(Paint.Style.STROKE);
        mMarkerPaint2.setStrokeWidth(Utils.dip2px(getContext(), 3));

//        float intervals[] = new float[]{5, 5, 5, 5};
//        PathEffect effects = new DashPathEffect(intervals, 1);

        // 内部同心圆画笔
        mMarkerPaint3 = new Paint();
        mMarkerPaint3.setAntiAlias(true);
        mMarkerPaint3.setStyle(Style.STROKE);
        mMarkerPaint3.setARGB(0xFF, 0x72, 0xB2, 0xFA);
        mMarkerPaint3.setStrokeWidth(Utils.dip2px(getContext(), 1));
//        mMarkerPaint3.setPathEffect(effects);

        // 分割线画笔
        mMarkerPaint4 = new Paint();
        mMarkerPaint4.setAntiAlias(true);
        mMarkerPaint4.setStyle(Paint.Style.STROKE);
        mMarkerPaint4.setARGB(0xFF, 0x72, 0xB2, 0xFA);
        mMarkerPaint4.setStyle(Paint.Style.STROKE);
        mMarkerPaint4.setStrokeWidth(Utils.dip2px(getContext(), 1));
//        mMarkerPaint4.setPathEffect(effects);

        // 朝北向画笔
//        northPaint = new Paint();
//        northPaint.setColor(Color.GRAY);
//        northPaint.setTextSize(40);
//        northHeight = (int) northPaint.measureText("N");

        // 度数画笔
//        textPaint = new Paint();
//        textPaint.setColor(Color.GRAY);

        // 小卫星画笔
        mpaint = new Paint();
        mpaint.setAntiAlias(true);
        mpaint.setStyle(Paint.Style.FILL);

        mpaint.setTextSize(Utils.dip2px(getContext(), 18));
        if (SysUtils.getInstance().is240x320) {
            mpaint.setTextSize(9);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        android:background="@drawable/satellite_bg"
        drawEarth(canvas);
        drawSatellites(canvas);
    }

    /**
     * 绘制地图同心圆
     *
     * @param canvas
     */
    private void drawEarth(Canvas canvas) {
        canvas.drawCircle(mnWidth / 2, mnHegiht / 2, raduis, mMarkerPaint);
        canvas.drawCircle(mnWidth / 2, mnHegiht / 2, raduis, mMarkerPaint2);
        canvas.drawCircle(mnWidth / 2, mnHegiht / 2, raduis * 2 / 3,
                mMarkerPaint3);
        canvas.drawCircle(mnWidth / 2, mnHegiht / 2, raduis / 3, mMarkerPaint3);

        canvas.drawLine(mnWidth / 2, mnHegiht / 2 - raduis, mnWidth / 2,
                mnHegiht / 2 + raduis, mMarkerPaint4);
        canvas.drawLine(mnWidth / 2 - raduis, mnHegiht / 2, mnWidth / 2
                + raduis, mnHegiht / 2, mMarkerPaint4);

        int w = (int) (raduis * Math.sin(Math.PI / 6));
        int h = (int) (raduis * Math.cos(Math.PI / 6));
        // 30度线
        canvas.drawLine(mnWidth / 2 + w, mnHegiht / 2 - h, mnWidth / 2 - w,
                mnHegiht / 2 + h, mMarkerPaint4);
        // // 30度

        // 330度线
        canvas.drawLine(mnWidth / 2 - w, mnHegiht / 2 - h, mnWidth / 2 + w,
                mnHegiht / 2 + h, mMarkerPaint4);

        w = (int) (raduis * Math.sin(Math.PI / 3));
        h = (int) (raduis * Math.cos(Math.PI / 3));
        // 60度线
        canvas.drawLine(mnWidth / 2 + w, mnHegiht / 2 - h, mnWidth / 2 - w,
                mnHegiht / 2 + h, mMarkerPaint4);
        // 300度线
        canvas.drawLine(mnWidth / 2 - w, mnHegiht / 2 - h, mnWidth / 2 + w,
                mnHegiht / 2 + h, mMarkerPaint4);

        // N
//        canvas.drawText("N", mnWidth / 2 - northHeight / 2, mnHegiht / 2
//                - raduis * 5 / 6 + northHeight / 2, northPaint);
    }

    private void drawSatellites(Canvas canvas) {
        mgpsAllStatus = GPSProvider.getInstance().GetSatellite();
        if (null == mgpsAllStatus)
            return;
        Iterator<GpsSatellite> it = mgpsAllStatus.iterator();
        while (it.hasNext()) {
            GpsSatellite gpsState = it.next();

            drawSatellite(canvas, gpsState);
        }
    }

    /**
     * 绘制小卫星
     *
     * @param canvas
     * @param gpsState
     */
    private void drawSatellite(Canvas canvas, GpsSatellite gpsState) {

        double dElevation = gpsState.getElevation();
        dElevation = (raduis - mnStarRadius) * Math.cos(dElevation * 2 * Math.PI / 360);

        double dAzimuth = gpsState.getAzimuth();
        int nStartX = (int) (dElevation * Math.sin(Math.PI * dAzimuth / 180))
                + mnWidth / 2;
        int nStartY = -(int) (dElevation * Math.cos(Math.PI * dAzimuth / 180))
                + mnHegiht / 2;

        mpaint.setStrokeWidth(Utils.dip2px(getContext(), 2));
        int s = (int) gpsState.getSnr();
        if (s <= 0) {
            return;
        }
        if (gpsState.usedInFix()) {
            mpaint.setColor(Color.GREEN);
        } else {
            if (s >= 30)
                mpaint.setColor(Color.YELLOW);
            else
                mpaint.setColor(Color.RED);
        }

        int y0 = 0;
        if (gpsState.getPrn() > 200) {
            //北斗 三角形
            int r = mnStarRadius;
            Path path3 = new Path();
            path3.moveTo(nStartX, nStartY - r);
            path3.lineTo(nStartX + r, nStartY + r);
            path3.lineTo(nStartX - r, nStartY + r);
            path3.close();
            //绘制三角形
            canvas.drawPath(path3, mpaint);
            y0 = 5;
        } else if (gpsState.getPrn() > 30) {
            //格洛纳斯 正方形
            canvas.drawRect(nStartX - mnStarRadius, nStartY - mnStarRadius, nStartX + mnStarRadius, nStartY + mnStarRadius, mpaint);
        } else {
            //GPS 圆形
            canvas.drawCircle(nStartX, nStartY, mnStarRadius, mpaint);
        }

        mpaint.setStrokeWidth(0);
        String strPrn = String.valueOf(gpsState.getPrn());
        float textWidth = mpaint.measureText(strPrn) / 2;
        float textHeight = mpaint.getTextSize() / 2;
        mpaint.setColor(Color.BLACK);
        canvas.drawText(strPrn, nStartX - textWidth, nStartY + textHeight - 1 + y0, mpaint);

    }

    public void updateDegree(float fDegree) {
        updateOffset();
        this.mfDegree = fDegree;
        this.postInvalidate();
    }

    public void updateView(int nHeight, int nWidth) {
        // mnHegiht = nHeight;
        // mnWidth = nWidth;
    }

    public void recycle() {
        if (mbmpEarth != null) {
            mbmpEarth.recycle();
            mbmpEarth = null;
        }
        if (mbmpStar != null) {
            mbmpStar.recycle();
            mbmpEarth = null;
        }
    }

}
